package booksystem.controller;

import booksystem.dao.BookDao;
import booksystem.dao.CartDao;
import booksystem.dao.impl.BookDaoImpl;
import booksystem.dao.impl.CartDaoImpl;
import booksystem.entity.Book;
import booksystem.entity.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "CartServlet", urlPatterns = "/cart")
public class CartServlet extends BaseServlet {
    private CartDao cartDao = new CartDaoImpl();
    /*
     * 获取购物车列表
     * */
    public void cartList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前用户信息
        HttpSession session = req.getSession();
        String user_id = (String) session.getAttribute("user_id");
        // 从数据库中获取用户的购物车信息
        List<Cart> cart_list = cartDao.getUserCart(user_id);
        List<String> book_name = new ArrayList<>();
        for (Cart cart : cart_list) {
            // 通过订单中的book_id 获取book表中的书籍信息
            BookDao bookDao = new BookDaoImpl();
            Book book = bookDao.getBookDetail(cart.getBook_id());
            book_name.add(book.getName());
        }

        req.setAttribute("cart_list", cart_list);
        req.setAttribute("book_name", book_name);
        // 将获取到的数据传到前端页面
        req.getRequestDispatcher("/shopping_cart.jsp").forward(req, resp);
    }

    /*
     * 向购物车中添加一条记录
     * */
    public void addCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取购物车字段
        String user_id = req.getParameter("user_id");
        String cover = req.getParameter("cover");
        String book_id = req.getParameter("book_id");
        String one_price = req.getParameter("one_price");
        String count = req.getParameter("buy_num");
//         通过单价和数量计算出总价
        double all_price = Double.parseDouble(one_price) * Double.parseDouble(count);

//        System.out.println(user_id+"->"+book_id+"->"+cover+"->"+one_price+"->"+count+"->"+all_price);
        // 将数据保存到数据库中
        Cart cart = new Cart();
        cart.setUser_id(user_id);
        cart.setCover(cover);
        cart.setBook_id(book_id);
        cart.setOne_price(one_price);
        cart.setCount(count);
        cart.setAll_price(String.valueOf(all_price));
        cartDao.addBookToCart(cart);

        // 给Ajax返回信息
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String str = "{\"msg\":\"添加成功,请在购物车中查看\",\"status\":true}";
        out.print(str);
        out.flush();
        out.close();
//        req.getRequestDispatcher("/book?m=detail").forward(req, resp);


    }
    /*
     * 从购物车中删除一条记录
     * */
    public void deleteCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 删除购物车里的图书
        CartDao cartDao = new CartDaoImpl();
        int cart_id = Integer.parseInt(req.getParameter("cart_id"));
        cartDao.deleteBookFromCart(cart_id);
//        cartList(req, resp);
        resp.sendRedirect("/cart?m=cartList");
    }
}
